﻿Public Class Creditos 
    Dim x As Integer = 0
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
            
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            Modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True
         

        End If
    End Sub
    Private Sub Creditos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.clientes' table. You can move, or remove it, as needed.
        Me.ClientesTableAdapter.Fill(Me.DbFireAndIceDataSet.clientes)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.facturas' table. You can move, or remove it, as needed.
        Me.FacturasTableAdapter.Fill(Me.DbFireAndIceDataSet.facturas)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.usuarios' table. You can move, or remove it, as needed.
        Me.UsuariosTableAdapter.Fill(Me.DbFireAndIceDataSet.usuarios)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.nominastipo' table. You can move, or remove it, as needed.
        Me.NominastipoTableAdapter.Fill(Me.DbFireAndIceDataSet.nominastipo)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.creditos' table. You can move, or remove it, as needed.
        Me.CreditosTableAdapter.Fill(Me.DbFireAndIceDataSet.creditos)
        IduserTextBox.Text = My.Forms.MDI.Iduser.Caption()

    End Sub

    Private Sub MontocreditoTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = True
            MsgBox("Solo puede ingresar números", MsgBoxStyle.Information, "Creditos")
            MontocreditoTextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            MsgBox("No debe ingresar espacios", MsgBoxStyle.Information, "Creditos")
            MontocreditoTextBox.Focus()

        Else
            e.Handled = False
        End If
    End Sub

    Private Sub IdfacturaLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdfacturaLookUpEdit.EditValueChanged
        If Not IsDBNull(IdfacturaLookUpEdit) Then

            txtfechafact.EditValue = IdfacturaLookUpEdit.GetColumnValue("fecha")
            txtsubtotal.EditValue = IdfacturaLookUpEdit.GetColumnValue("subtotal")
            txttotal.EditValue = IdfacturaLookUpEdit.GetColumnValue("Total")

        End If
    End Sub

    Private Sub IdclienteLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdclienteLookUpEdit.EditValueChanged
        If Not IsDBNull(IdclienteLookUpEdit) Then
            txtnombrecliente.EditValue = IdclienteLookUpEdit.GetColumnValue("nombre")
            txtapellidocliente.EditValue = IdclienteLookUpEdit.GetColumnValue("apellido")
            txttelefonocliente.EditValue = IdclienteLookUpEdit.GetColumnValue("telefono")
            PictureEdit1.EditValue = IdclienteLookUpEdit.GetColumnValue("fotografia")
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

        End If
    End Sub

    Private Sub PanelArriba_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelArriba.Paint

    End Sub

    Private Sub MontocreditoTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub GroupControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl1.Paint

    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
       
        x = 0
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        x = 1

        Mostrar()
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        x = 0
        ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)
        ErrorProvider1.SetError(IdnominaLookUpEdit, Nothing)
        ErrorProvider1.SetError(IdfacturaLookUpEdit, Nothing)
        ErrorProvider1.SetError(MontocreditoTextBox, Nothing)
       
        Mostrar()
    End Sub

    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click



        If (x = 0) Then

            If IdnominaLookUpEdit.Text = "" Then

                ErrorProvider1.SetError(IdnominaLookUpEdit, "Seleccione un tipo de nómina")
            Else
                ErrorProvider1.SetError(IdnominaLookUpEdit, Nothing)
            End If


            If txtfechafact.Text = "" Then

                ErrorProvider1.SetError(IdfacturaLookUpEdit, "Seleccione una  factura")
            Else
                ErrorProvider1.SetError(IdfacturaLookUpEdit, Nothing)
            End If

            If MontocreditoTextBox.Text = "" Then
                ErrorProvider1.SetError(MontocreditoTextBox, "Escriba la cantidad de credito que se le brindará")

            Else
                ErrorProvider1.SetError(MontocreditoTextBox, Nothing)

            End If



            If (txtapellidocliente.Text = "") Then
                ErrorProvider1.SetError(IdclienteLookUpEdit, "Seleccione un cliente")
            Else
                ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)
             
            End If

            If (IdnominaLookUpEdit.Text <> "") And (txtfechafact.Text <> "") And (MontocreditoTextBox.Text <> "") And (txtapellidocliente.Text <> "") Then


                ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)
                ErrorProvider1.SetError(IdnominaLookUpEdit, Nothing)
                ErrorProvider1.SetError(IdfacturaLookUpEdit, Nothing)
                ErrorProvider1.SetError(MontocreditoTextBox, Nothing)
                CreditosTableAdapter.Insert(IdcreditoTextBox.Text, IdclienteLookUpEdit.EditValue, Date.Now, IdfacturaLookUpEdit.EditValue, MontocreditoTextBox.Text, IdnominaLookUpEdit.EditValue, My.Forms.MDI.Iduser.Caption.ToString)
                MessageBox.Show("Se guardó el resgistro satisfactoriamente")
                Mostrar()
            End If
        End If

        If x = 1 Then
            If IdnominaLookUpEdit.Text = "" Then

                ErrorProvider1.SetError(IdnominaLookUpEdit, "Seleccione un tipo de nómina")
            Else
                ErrorProvider1.SetError(IdnominaLookUpEdit, Nothing)
            End If


            If txtfechafact.Text = "" Then

                ErrorProvider1.SetError(IdfacturaLookUpEdit, "Seleccione una  factura")
            Else
                ErrorProvider1.SetError(IdfacturaLookUpEdit, Nothing)
            End If

            If MontocreditoTextBox.Text.Length = 0 Then
                ErrorProvider1.SetError(MontocreditoTextBox, "Escriba la cantidad de credito que se le brindará")

            Else
                ErrorProvider1.SetError(MontocreditoTextBox, Nothing)

            End If



            If (txtapellidocliente.Text = "") Then
                ErrorProvider1.SetError(IdclienteLookUpEdit, "Seleccione un cliente")
            Else

                ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)

            End If


            If (IdnominaLookUpEdit.Text <> "") And (txtfechafact.Text <> "") And (MontocreditoTextBox.Text <> "") And (txtapellidocliente.Text <> "") Then

                ErrorProvider1.SetError(IdclienteLookUpEdit, Nothing)
                ErrorProvider1.SetError(IdnominaLookUpEdit, Nothing)
                ErrorProvider1.SetError(IdfacturaLookUpEdit, Nothing)
                ErrorProvider1.SetError(MontocreditoTextBox, Nothing)
                MessageBox.Show("Se actualió el resgistro satisfactoriamente")
                Mostrar()
            End If
        End If



    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion2 As Integer
        opcion2 = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion2 = vbYes Then
            Try
                CreditosTableAdapter.DeleteQuerycreditos(IdcreditoTextBox.Text)
                Me.CreditosTableAdapter.Fill(Me.DbFireAndIceDataSet.creditos)
            Catch ex As Exception
                MsgBox("Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas", MsgBoxStyle.Critical, "Error")
            End Try


        End If
    End Sub
End Class